package com.sojson.util.mq.rabbit.immit;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import com.sojson.util.SpringUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.mq.rabbit.immit.interfaces.impl.RabbitBaseUtilImpl;
import com.sojson.util.mq.rabbit.immit.interfaces.impl.RabbitDelayMsgUtilImpl;
import com.sojson.util.mq.rabbit.immit.interfaces.impl.RabbitDirectUtilImpl;
import com.sojson.util.mq.rabbit.immit.interfaces.impl.RabbitFanoutUtilImpl;
import com.sojson.util.mq.rabbit.immit.interfaces.impl.RabbitHeadersUtilImpl;
import com.sojson.util.mq.rabbit.immit.interfaces.impl.RabbitTopicUtilImpl;

/**
 * Rabbit消息队列工具类
 * 
 * @author liu
 * @date 2021-07-11
 */
public class RabbitImmitUtil {

    private static AmqpAdmin amqp;
    private static RabbitTemplate rabbit;
    /** 消费者确认模式(自动确认) */
    public static final String ACK_MODE_AUTO = "AUTO";
    /** 消费者确认模式(手动确认) */
    public static final String ACK_MODE_MANUAL = "MANUAL";

    /**
     * 获取 交换机和队列的处理类
     * 
     * @return
     */
    public static AmqpAdmin getAmqp() {
        if (isBlankObject(amqp)) {
            amqp = SpringUtil.getBeanByType(AmqpAdmin.class);
        }
        return amqp;
    }

    /**
     * 获取 发送和接收消息的处理类
     * 
     * @return
     */
    public static RabbitTemplate getRabbit() {
        if (isBlankObject(rabbit)) {
            rabbit = SpringUtil.getBeanByType(RabbitTemplate.class);
        }
        return rabbit;
    }

    /**
     * 判断一个对象是否不是空(空对象)
     * 
     * @param obj
     * @return
     */
    private static boolean isBlankObject(Object obj) {
        return StringUtil.isBlankObject(obj);
    }

    /**
     * Rabbit基础工具类(无交换机)
     * 
     * @return
     */
    public static RabbitBaseUtilImpl getBase() {
        return RabbitBaseUtilImpl.getInstances();
    }

    /**
     * Rabbit-Direct(指定匹配)工具类
     * 
     * @return
     */
    public static RabbitDirectUtilImpl getDirect() {
        return RabbitDirectUtilImpl.getInstances();
    }

    /**
     * Rabbit-Topic(模糊匹配)工具类
     * 
     * @return
     */
    public static RabbitTopicUtilImpl getTopic() {
        return RabbitTopicUtilImpl.getInstances();
    }

    /**
     * Rabbit-Fanout(广播)工具类
     * 
     * @return
     */
    public static RabbitFanoutUtilImpl getFanout() {
        return RabbitFanoutUtilImpl.getInstances();
    }

    /**
     * Rabbit-Headers(用请求头(Map中的参数)指定匹配)工具类
     * 
     * @return
     */
    public static RabbitHeadersUtilImpl getHeaders() {
        return RabbitHeadersUtilImpl.getInstances();
    }

    /**
     * 延迟消息工具类
     * 
     * @return
     */
    public static RabbitDelayMsgUtilImpl getDelay() {
        return RabbitDelayMsgUtilImpl.getInstances();
    }

}